package 刷题7月.链表反转迭代;//0707
/*
*   1、将下一个节点指针保存到next变量 next = curr.next
    2、将下一个节点的指针指向prev，curr.next = prev
    3、准备处理下一个节点，将curr赋值给prev
    4、将下一个节点赋值为curr，处理一个节点
*
* */
public class ReverseList {
    static class ListNode{
        int val;
        ListNode next;
        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public static ListNode iterate(ListNode head){
        ListNode prev = null,curr,next;
        curr = head;
        while(curr != null){
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
    public static void main(String[] args) {
        ListNode node5 = new ListNode(5,null);
        ListNode node4 = new ListNode(4,node5);
        ListNode node3 = new ListNode(3,node4);
        ListNode node2 = new ListNode(2,node3);
        ListNode node1 = new ListNode(1,node2);
        ListNode node = iterate(node1);
        //可以使用debug方式查看每个结点的值及其指针
        System.out.println(node);
    }
}
